package com.wzy.spring2.util;

import com.google.common.base.Joiner;
import com.google.common.collect.Lists;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

public class FileUtils {

    public static void main(String[] args) {
        String s;
        String writePath = "/Users/weizhengyun/Desktop/test1.csv";
        List<String> nameList = Lists.newArrayList("魏正昀", "林巧稚");
        FileUtils.writeFile(writePath, Joiner.on(",").join(nameList));

        String readPath = writePath;
        FileUtils.readFile(readPath);
    }

    public static void readFile(String filePath) {
        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String line;

            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void writeFile(String filePath, String data) {
        try (FileWriter writer = new FileWriter(filePath)) {
            writer.write(data);
            System.out.println("数据已成功写入文件。");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
